﻿#include "OperMaterialFoam.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIDialog/GUIPhyscisDialog/MaterialDialogFoam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFoam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"

#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

namespace ModelOper
{
    bool ModelOper::OperMaterialFoam::execGUI()
    {
        //按钮点击事件触发
        if (_emitter == nullptr) return false;
        const QString name = _emitter->objectName();
        if (name == "actionFoam")
            createMaterial();
        return false;
    }

    bool ModelOper::OperMaterialFoam::execProfession()
    {
        return false;
    }

    void ModelOper::OperMaterialFoam::createMaterial()
    {
        //弹出对话框
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        GUI::MaterialDialogFoam dlg(GUI::PhysicsDlgExecType::Create, -1, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();

    }

    void ModelOper::OperMaterialFoam::duplicateMaterial(const int matID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::MaterialDialogFoam dlg(GUI::PhysicsDlgExecType::Duplicate, matID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void ModelOper::OperMaterialFoam::editMaterial(const int matID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::MaterialDialogFoam dlg(GUI::PhysicsDlgExecType::Edit, matID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void ModelOper::OperMaterialFoam::removeMaterial(const int matid)
    {
        //获取管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();
        if (matMgr == nullptr) return;
        //移除并更新列表
        matMgr->removeDataByID(matid);
        ModelOper::updateBrowserTable();
    }

    void OperMaterialFoam::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& matIDs)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();
        if (matMgr == nullptr) return;
        //设置对话框
        GUI::AttributeSetDialog dlg(1, mw);
        dlg.setTextLabel(name);
        dlg.setDefaultValueDouble(text.toDouble());
        if (dlg.exec() != QDialog::Accepted) return;

        const double value = dlg.getValueDouble();

        for (int id : matIDs)
        {
            Acoustics::FITKAcousticsMaterialsFoam* Isosolid = dynamic_cast
                <Acoustics::FITKAcousticsMaterialsFoam*>(matMgr->getDataByID(id));
            if (Isosolid == nullptr) continue;
            //不同列设置不同参数
            Isosolid->setDensity(value);
        }
        ModelOper::updateBrowserTable();
    }


}
